<template>
	<view class="search-page">
		<!-- 顶部搜索栏 -->
		<view class="search-header">
			<view class="search-input-box">
				<input 
					type="text" 
					v-model="searchText" 
					placeholder="贡嘎" 
					placeholder-class="placeholder"
					focus
					@input="onSearch"
					@confirm="handleSearch"
				/>
				<text class="clear-icon" v-if="searchText" @tap="clearSearch">×</text>
			</view>
			<text class="search-btn" @tap="handleSearch">搜索</text>
		</view>

		<!-- 搜索结果 -->
		<view class="search-result" v-if="searchText">
			<!-- 加载中 -->
			<view class="loading-box" v-if="isLoading">
				<view class="loading"></view>
				<text>搜索中...</text>
			</view>
			
			<!-- 搜索结果列表 -->
			<view class="itemboy" v-else>
				<view
					v-if="searchResults && searchResults.length > 0"
					class="describeCard"
					v-for="(item, index) in searchResults"
					:key="index"
					@click="toDetail(item.shop_id)"
				>
					<view class="imgBox">
						<image :src="imageUrl + item.cover_image" mode=""></image>
						<view class="labelFlex">
							<view class="label">
								<image
									src="@/static/home/icon_design-tool_drafting-compass.png"
									mode=""
								></image>
								<text>{{ item.trip_day }}天{{ item.trip_night }}晚</text>
							</view>
							<text class="Selection">西望甄选</text>
						</view>
					</view>
					<view class="textBox">
						<text class="title">{{ item.title }}</text>
						<text class="deputyTitle">{{ item.subtitle }}</text>
						<text class="content">{{ item.description }}</text>
						<view class="tags">
							<view
								class="tags-card"
								v-for="(item_a, index_a) in item.label_list"
								:key="index_a"
							>
								<text>{{ item_a }}</text>
							</view>
						</view>
						<view class="footer-box">
							<view class="price">
								<text>￥</text>
								<text>{{ item.display_price }}</text>
								<text class="NumberPeople">/人起</text>
							</view>
						</view>
					</view>
				</view>
				<view v-else-if="hasSearched" class="noinfo">
					<text class="empty-icon">暂无数据</text>
					<text class="sub-text">没有找到相关行程，换个关键词试试吧~</text>
				</view>
			</view>
		</view>

		<!-- 搜索历史 -->
		<view class="search-history" v-if="!searchText">
			<view class="history-header">
				<text class="title">历史搜索</text>
					<!-- <text class="iconfont icon-delete"></text> -->
					<uni-icons type="trash" color="#999" size="24" @tap="clearHistory"></uni-icons>

			</view>
			<view class="history-tags">
				<text 
					class="tag" 
					v-for="(item, index) in historyList" 
					:key="index"
					@tap="selectHistory(item)"
				>{{item}}</text>
			</view>
		</view>

		<!-- 热门搜索 -->
		<view class="hot-search" v-if="!searchText">
			<view class="hot-header">
				<text class="title">热门搜索</text>
			</view>
			<view class="hot-tags">
				<text 
					class="tag" 
					v-for="(item, index) in hotList" 
					:key="index"
					@tap="selectHot(item)"
				>{{item}}</text>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { searchShop } from '@/config/API/home.js'
import { imageUrl } from "../../config/request/request";
import { routerGuard } from "@/config/RouteGuard.js";

const searchText = ref('')
const historyList = ref([])
const hotList = ref(['双日落', '牛背山', '川西', '四姑娘山'])

const searchResults = ref([])
const hasSearched = ref(false)
const isLoading = ref(false)
let searchTimer = null

// 从本地存储加载搜索历史
const loadSearchHistory = () => {
	const history = uni.getStorageSync('searchHistory')
	historyList.value = history ? JSON.parse(history) : []
}

// 保存搜索历史到本地存储
const saveSearchHistory = () => {
	uni.setStorageSync('searchHistory', JSON.stringify(historyList.value))
}

// 在页面加载时读取历史记录
onMounted(() => {
	loadSearchHistory()
})

// 搜索输入
const onSearch = (e) => {
	searchText.value = e.detail.value
	if (!searchText.value) {
		hasSearched.value = false
		searchResults.value = []
		return
	}
	
	// 防抖处理
	if (searchTimer) clearTimeout(searchTimer)
	searchTimer = setTimeout(() => {
		handleSearch()
	}, 300)
}

// 执行搜索
const handleSearch = async () => {
	if (!searchText.value) return
	
	try {
		isLoading.value = true
		const res = await searchShop(searchText.value)
		if (res.code === 200) {
			searchResults.value = res.data
			hasSearched.value = true
			// 将搜索词添加到历史记录
			if (!historyList.value.includes(searchText.value)) {
				historyList.value.unshift(searchText.value)
				// 限制历史记录数量
				if (historyList.value.length > 10) {
					historyList.value = historyList.value.slice(0, 10)
				}
				// 保存到本地存储
				saveSearchHistory()
			}
		} else {
			uni.showToast({
				title: res.msg || '搜索失败',
				icon: 'none'
			})
		}
	} catch (error) {
		console.error('搜索错误:', error)
		uni.showToast({
			title: '搜索出错，请重试',
			icon: 'none'
		})
	} finally {
		isLoading.value = false
	}
}

// 清空搜索
const clearSearch = () => {
	searchText.value = ''
	hasSearched.value = false
	searchResults.value = []
	if (searchTimer) clearTimeout(searchTimer)
}

// 清空历史记录
const clearHistory = () => {
	uni.showModal({
		title: '提示',
		content: '确定要清空搜索历史吗？',
		success: (res) => {
			if (res.confirm) {
				historyList.value = []
				// 清除本地存储
				uni.removeStorageSync('searchHistory')
			}
		}
	})
}

// 选择历史记录
const selectHistory = (item) => {
	searchText.value = item
	handleSearch()
}

// 选择热门搜索
const selectHot = (item) => {
	searchText.value = item
	handleSearch()
}


//点击进入详情
const toDetail = (id) => {
  const url = `/homeSubpages/ProductDetails/index?ShopId=${id}`;
  routerGuard(url);
};
</script>

<style lang="scss" scoped>
.search-page {
	min-height: 100vh;
	background: #fff;
	padding: 0 32rpx;
	
	.search-header {
		position: sticky;
		top: 0;
		background: #fff;
		z-index: 100;
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		
		.search-input-box {
			flex: 1;
			height: 80rpx;
			background: #F5F5F5;
			border-radius: 32rpx;
			display: flex;
			align-items: center;
			padding: 0 24rpx;
			margin-right: 20rpx;
			
			.icon-search {
				font-size: 32rpx;
				color: #999;
				margin-right: 10rpx;
			}
			
			input {
				flex: 1;
				height: 100%;
				font-size: 28rpx;
			}
			
			.clear-icon {
				font-size: 40rpx;
				color: #999;
				padding: 0 20rpx;
				font-weight: 300;
			}
			
			.placeholder {
				color: #999;
			}
		}
		
		.search-btn {
			font-size: 28rpx;
			color: #333;
			background: #F5F5F5;
			padding: 20rpx 32rpx;
			border-radius: 32rpx;
		}
	}
	
	.search-result {
		margin-top: 20rpx;
		.itemboy {
			width: 100%;
			transition: all 0.5s;
			padding: 0;
			box-sizing: border-box;

			.describeCard {
				width: 100%;
				min-height: 450rpx;
				background-color: #fff;
				border-radius: 20rpx 20rpx 45rpx 45rpx;
				overflow: hidden;
				margin-bottom: 36rpx;
				border: 1rpx solid #dedede;

				.imgBox {
					width: 100%;
					height: 370rpx;
					position: relative;

					image {
						width: 100%;
						height: 100%;
						border-radius: 14rpx 14rpx 0 0;
					}

					.labelFlex {
						display: flex;
						align-items: center;
						gap: 15rpx;
						position: absolute;
						top: 25rpx;
						left: 20rpx;
						height: 30rpx;

						.label {
							display: flex;
							align-items: center;
							justify-content: center;
							padding: 0 10rpx;
							gap: 4rpx;
							height: 100%;
							border-radius: 6rpx;
							background: rgba(54, 48, 48, 1);
							font-size: 16rpx;
							font-weight: 400;
							color: rgba(232, 208, 149, 1);
							box-sizing: border-box;

							image {
								width: 20rpx;
								height: 20rpx;
							}
						}

						.Selection {
							font-size: 16rpx;
							font-weight: 400;
							line-height: 30rpx;
							color: #fff;
							background-color: rgba(4, 75, 173, 1);
							padding: 0rpx 10rpx;
							border-radius: 6rpx;
						}
					}
				}

				.textBox {
					width: 100%;
					padding: 26rpx;
					padding-bottom: 20rpx;
					box-sizing: border-box;

					text {
						display: block;
					}

					.title {
						font-size: 28rpx;
						font-weight: 700;
					}

					.deputyTitle {
						font-size: 26rpx;
						font-weight: 500;
					}

					.content {
						margin: 18rpx 0;
						font-size: 24rpx;
						color: rgba(125, 125, 125, 1);
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 7;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.tags {
						width: 100%;
						display: flex;
						flex-wrap: wrap;
						gap: 10rpx;

						.tags-card {
							padding: 10rpx 12rpx;
							border-radius: 12rpx;
							background-color: #f5f6f7;
							box-sizing: border-box;

							text {
								color: #888888;
								font-size: 24rpx;
							}
						}
					}

					.footer-box {
						width: 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;
						margin: 20rpx 0;

						.price {
							display: flex;

							text {
								font-weight: 600;
							}

							.NumberPeople {
								margin-left: 10rpx;
								color: rgba(125, 125, 125, 1);
								font-weight: 500;
								font-size: 28rpx;
							}
						}
					}
				}
			}

			.noinfo {
				width: 100%;
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 100rpx 0;
				
				.empty-icon {
					font-size: 32rpx;
					color: #999;
					margin-bottom: 20rpx;
				}
				
				.sub-text {
					font-size: 26rpx;
					color: #999;
				}
			}
		}
	}
	
	.search-history {
		margin-top: 40rpx;
		
		.history-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 20rpx;
			
			.title {
				font-size: 28rpx;
				color: #333;
				font-weight: 500;
			}
			
			.delete-icon {
				color: #999;
				font-size: 32rpx;
			}
		}
		
		.history-tags {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
			
			.tag {
				padding: 16rpx 32rpx;
				background: #F5F5F5;
				border-radius: 12rpx;
				font-size: 26rpx;
				color: #333;
			}
		}
	}
	
	.hot-search {
		margin-top: 60rpx;
		
		.hot-header {
			margin-bottom: 20rpx;
			
			.title {
				font-size: 28rpx;
				color: #333;
				font-weight: 500;
			}
		}
		
		.hot-tags {
			display: flex;
			flex-wrap: wrap;
			gap: 20rpx;
			
			.tag {
				padding: 16rpx 32rpx;
				background: #F5F5F5;
				border-radius: 12rpx;
				font-size: 26rpx;
				color: #333;
			}
		}
	}
	
	.loading-box {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 40rpx 0;
		
		.loading {
			width: 40rpx;
			height: 40rpx;
			border: 4rpx solid #f3f3f3;
			border-top: 4rpx solid #333;
			border-radius: 50%;
			animation: spin 1s linear infinite;
			margin-bottom: 20rpx;
		}
		
		text {
			font-size: 28rpx;
			color: #999;
		}
	}
}

@keyframes spin {
	0% { transform: rotate(0deg); }
	100% { transform: rotate(360deg); }
}
</style> 